//
//  VISTAInfoDetailVC.m
//  看杂志
//
//  Created by zcj on 15-12-14.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "VISTAInfoDetailVC.h"
#import "VISTACustomTabBar.h"
#import "VISTACustomWebBar.h"

@interface VISTAInfoDetailVC ()

@end

@implementation VISTAInfoDetailVC
{
    UIWebView *_webView;
    BOOL isLoading;
    NSString *textId;
    UIActivityIndicatorView *_aiv;
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar removeFromSuperview];
    UIView *tabr = (VISTACustomTabBar *)[self.tabBarController.view viewWithTag:757];
    [tabr setHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    UIView *tabr = (VISTACustomTabBar *)[self.tabBarController.view viewWithTag:757];
    [tabr setHidden:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.flage == 1) {
        textId = @"5287";
    }else{
        VISTAInfoTableViewModel * dataSourceModel = self.dataSourceArray[_indexPathRow];
        textId = dataSourceModel.ID;
    }
    
    NSLog(@"%@",textId);
    [self createWebView];
    [self loadPage];
    [self configNavi];
    [self bottomViews];
    // Do any additional setup after loading the view.
}
/**
 *  定制导航栏
 */
- (void)configNavi
{
    //定制导航栏左按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 50, 32);
    [leftButton setImage:[UIImage imageNamed:@"返回_1"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"返回_2"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    //导航栏标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    titleLabel.text = @"资讯";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
}
/**
 *  pop回上一界面(infoVC)
 */
- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  创建最下面的按钮组
 */
- (void)bottomViews
{
    
    VISTACustomWebBar *webBar = [[VISTACustomWebBar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-35, self.view.frame.size.width, 35) ];
    webBar.delegate = self;
    [self.view addSubview:webBar];
}

#pragma mark VISTAWebBar Delegate
/**
 *  自定义按钮组代理方法
 *
 *  @param webBar 按钮组
 *  @param button 按下的按钮
 *  @param index  按下的按钮的序号
 */
-(void)customWebBar:(VISTACustomWebBar *)webBar didClickButton:(UIButton *)button WithButtonIndex:(NSUInteger)index
{
    switch (index) {
        case 0://退后按钮
            if (_indexPathRow > 0) {
                VISTAInfoTableViewModel * dataSourceModel = self.dataSourceArray[_indexPathRow--];
                textId = dataSourceModel.ID;
                _indexPathRow--;
                [self loadPage];
            }else if (_indexPathRow == 0){
                return ;
            }
            break;
        case 1://评论按钮
            
            break;
        case 2://转发按钮
            
            break;
        case 3://收藏按钮
            
            break;
        case 4://前进按钮
            if (_indexPathRow < self.dataSourceArray.count - 1) {
                VISTAInfoTableViewModel * dataSourceModel = self.dataSourceArray[_indexPathRow++];
                textId = dataSourceModel.ID;
                _indexPathRow++;
                [self loadPage];
            }else if (_indexPathRow == self.dataSourceArray.count-1){
                return ;
            }
            break;
            
        default:
            break;
    }
}
/**
 *  创建网页视图
 */
- (void)createWebView
{
    _webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    _webView.scrollView.bounces = NO;
    _webView.delegate = self;
    [self.view addSubview:_webView];
    [self createAIV];
    
}
#pragma mark webViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [_aiv startAnimating];
    //状态栏网络活动指示器
    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_aiv stopAnimating];
    //状态栏网络活动指示器
    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = !app.networkActivityIndicatorVisible;
}
/**
 *  活动指示器
 */
- (void)createAIV
{
    _aiv = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _aiv.center = self.view.center;
    _aiv.color = [UIColor redColor];
    [self.view addSubview:_aiv];
}
/**
 *  加载网页
 */
- (void)loadPage
{
    NSString *font = @"l";
    NSString *url1 = @"http://ktx.cms.palmtrends.com/api_v2.php?%20action=article&uid=10067567&id=";
    NSString *url3 = @"&mobile=iphone5%20&e=40dab97d773e7860febfc897c04824e2&fontsize=";
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@%@",url1,textId,url3,font]]];
    NSLog(@"textid  == %@",textId);
    [_webView loadRequest:urlRequest];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
